package com.design.designmode.action.observe;

import java.util.ArrayList;
import java.util.List;

/**
 * @author hxl
 * @since 2023-04-18
 */
public class ConcreteSubject implements Subject {
    private String name;
    private String state;

    private List<Observer> observerList;


    public ConcreteSubject(String name) {
        state = "未更新";
        this.name = name;
        observerList = new ArrayList<Observer>();
    }


    @Override
    public void attach(Observer observer) {
        observerList.add(observer);
    }

    @Override
    public void detach(Observer observer) {
        observerList.remove(observer);
    }

    @Override
    public void sendMsg() {
        for (Observer observer : observerList) {
            observer.update();
        }
    }

    @Override
    public void setState(String state) {
        this.state = state;

        System.out.println(name + "的状态发生变化，变化后的状态为：" + state);
        sendMsg();

    }

    @Override
    public String getState() {
        return state;
    }
}
